<?php
namespace Home\Controller; 
use Think\Controller;
class UserController extends Controller {
    public function index(){
        $user = M('user');
        $uid = session("uid");
        $userInfo = $user->where(array("uid"=>$uid))->select();   
        if($userInfo) {
            $message['status'] = true;
            $message['data'] = $userInfo;
        }else {
            $message['status'] = false;
            $message['data'] = '';
        }
        $this->ajaxReturn($message);
    } 

    public function updateInfo() {        
        $user = M('user');
        $data['uid'] = session("uid");
        $data['userName'] = I('post.userName', '');
        $data['wish'] = I('post.wish', '');
        $data['lastip'] = I('post.password', '');
        $data['birthday'] = I('post.birthday', '');
        $data['address'] = I('post.address', '');
        $data['phone'] = I('post.phone', '');
        $data['gender'] = I('post.gender', '');
        $userInfo = $user->where(array("uid"=>$data['uid']))->find(1);
        if(empty($userInfo)) {
            $user->data($data)->save();
            $message['status'] = true;
            $message['message'] = "信息修改成功！";
        }else {
            $message['status'] = false;
            $message['message'] = "信息修改失败！";
        }
         
        $this->ajaxReturn($message);
    }

    public function updatePassword() {
        $user = M('user');
        $data['uid'] = session("uid");
        $data['password'] = I('post.newPassword', '');
        $password = I('post.password', '');
        $userInfo = $user->where(array("uid"=>$data['uid']))->find();
        if($userInfo["password"] == $password) {        
            $user->data($data)->save();
            $message['message'] = "密码修改成功！";
            $message['status'] = true;
        }else {
            $message['status'] = false;
            $message['message'] = "旧密码有误，请重新输入！";
        }

        $this->ajaxReturn($message);
    }

    public function updatePhoto() {       
        $config = array(
            'maxSize'       => 5 * 1024 * 1024,
            'rootPath'      => './Public/',
            'saveName'      => array('uniqid', ''),
            'driver'        => 'Qiniu',
            'driverConfig'  => array(
                                'secretKey' => 'BEchWQ-79ubI-6vk1znIBOHITk9bmvs1X4TWhzIw', 
                                'accessKey' => '7DA6BNM5NRKT2og6KSz6CfcUZdR5TT0L_CkeMveK',
                                'domain'    => 'ogrv04tc4.bkt.clouddn.com',
                                'bucket'    => 'photo', 
                            )
        );
        //上传成功
        $upload = new \Think\Upload($config);        
        $info = $upload->uploadOne($_FILES['photo']);

        //保存到数据库
        $user = M('user');
        $data['uid'] = session("uid");        
        $data['photo'] = $info["url"];
        $user->data($data)->save();

        //返回数据
        $this->ajaxReturn($info);
    }

    //获取文章列表
    public function getBlog(){    
        $nowPage = I('post.nowPage', '');
        $count = I('post.count', '');
        $post = D('post');
        $uid = session("uid");
        $result['articleList'] = $post->where(array('post_uid'=> $uid))->order('post_createtime desc')->page($nowPage, $count)->select();

        if(count($result['articleList']) < $count) {
            $result['isMore'] = false;      		
        }else {
            $result['isMore'] = true;
        }
        $this->ajaxReturn($result);
    }

     //删除文章
     public function delBlog(){    
        $post_id = I('post.post_id', '');
        $post = D('post');
        $result = $post->where(array('post_id'=> $post_id))->delete();
        $this->ajaxReturn($result);
    }
}


